package com.weiaibenpao.demo.chislim.adater;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.bean.Li_MarathonResult;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2016/10/11.
 */
public class MarathonAdapter extends RecyclerView.Adapter<MarathonAdapter.ViewHolder> {

    private ArrayList marathonList;
    Context context;
    private LayoutInflater mInflater;
    private  OnItemClickListener mOnItemClickListener;
    //设置点击和长按事件

    public interface OnItemClickListener {
        void onItemClick(View view, Li_MarathonResult.DataBean.MarathonlistBean dataBean); //设置点击事件
    }
    public  void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    /**
     * 构造器
     * @param context
     */
    public MarathonAdapter(Context context) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        marathonList = new ArrayList();
    }

    /**
     * item显示类型
     * 引入布局
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_marathonlist, parent, false);
        //view.setBackgroundColor(Color.RED);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    /**
     * 数据的绑定显示
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        //绑定事件
        if (mOnItemClickListener != null) {
            holder.marathLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(holder.marathLayout, (Li_MarathonResult.DataBean.MarathonlistBean) marathonList.get(position));
                }
            });
        }
        Picasso.with(context).load(((Li_MarathonResult.DataBean.MarathonlistBean)marathonList.get(position)).getMContentImg()).into(holder.marathImg);
        holder.marathonTitle.setText(((Li_MarathonResult.DataBean.MarathonlistBean)marathonList.get(position)).getMarathonTitle());
        holder.marathonIntro.setText(((Li_MarathonResult.DataBean.MarathonlistBean)marathonList.get(position)).getBadgeIntro());
        holder.userNum.setText(((Li_MarathonResult.DataBean.MarathonlistBean)marathonList.get(position)).getJoinNumber() + "人参与");
    }

    public void refreshData(List<Li_MarathonResult.DataBean.MarathonlistBean> data) {

        marathonList = (ArrayList) data;

        this.notifyDataSetChanged();
    }
    /**
     * 要显示的item数目
     * @return
     */
    @Override
    public int getItemCount() {
        return marathonList.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView marathImg;
        public TextView marathonTitle;
        public TextView marathonIntro;
        public TextView userNum;

        public RelativeLayout marathLayout;
        public ViewHolder(View view) {
            super(view);

            marathImg = (ImageView) view.findViewById(R.id.marathImg);
            marathonTitle = (TextView) view.findViewById(R.id.marathonTitle);
            marathonIntro = (TextView) view.findViewById(R.id.marathonIntro);
            userNum = (TextView) view.findViewById(R.id.userNum);

            marathLayout = (RelativeLayout) view.findViewById(R.id.marathLayout);
        }
    }
}